<?php
/**
 * Medical Study Followup Add - Popup
 * This module will display a search help
 * to help the user choose in the followup list
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy
 * @license #license#
 * 
 */

use Nawa\MedicalStudy\Followup;
use Nawa\MedicalStudy;
use Nawa\MedicalStudy\MedicalCenter;
use Nawa\Customer;
use Nawa\MedicalStudy\Role;
use Fox\Html\CheckBox;
use Fox\Dao\SqlFragment;
use Fox\Html\Input;
use Fox\Html\TextArea;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;

$xview = Xview::instance();
$msid = Xview::instance()->val('msid');

$searchHelp = new Fox\Html\SearchHelp('MedicalStudyFollowup');
$searchHelp->width(500);
$searchHelp->setMultiple(new Fox\Html\Button('select',Generator::reloadFromSelectedLines(
							'#b_GRMedicalStudyFollowup',
							'MC_followup','',
							"hide(\'sh:MedicalStudyFollowup\')"
							)));
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();
// Load already selected Items
$ms = $xview->component('medicalstudies','object',array('msid' => $msid));
$followups = Fox\Dao::finder(Followup::className())
	->where('msid = ?',array($msid))
		->follow(Followup::LINK_ITEMS)
		->where('type = ? AND language = ?',array(Followup\Item::TYPE_UNIQUE,$xview->language()))
		->find();
foreach($followups as $item) {
	$excluded[] = $item->followupid;
}

if(isset($_POST['dbcnt']))
{
	$inputs = $searchHelp->screen()->get();
	$finder = new Followup\Item();
	
	$fragments[] = $searchHelp->screen()->fragments();
	if(isset($excluded)) {
		$fragments[] = new SqlFragment('followupid NOT IN (?)',
							array($excluded),
							array(Connection::PARAM_INT_ARRAY));
	}
	$fragments[] = new SqlFragment('language = ? AND active = ?',
						array($xview->language(),1));
	// Handle Attaching a partner in the medical center view 
	// In this case if the filter is on we will only display partner that are attached
	// to the medical study
	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),array('label' => false),null,$inputs['dbcnt']);
	$searchHelp->grid()->initialize($res);
	echo $searchHelp->grid()->Display();
}
else {
	echo $searchHelp->display();
}